/**
 * Copyright (2024, ) Institute of Software, Chinese Academy of Sciences
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 *limitations under the License.
 */
package cn.ac.iscas.targets;

import java.util.logging.Logger;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.fasterxml.jackson.databind.JsonNode;

import cn.ac.iscas.core.AbstractTarget;
import jakarta.persistence.EntityManager;

/**
 * Http的配置文件，又可细分为多种
 * - username和password
 * - token
 * - cookie
 * - cert
 * 
 * @author wuheng@iscas.ac.cn
 * @since 1.0.0
 * 
 */
public abstract class RDBTarget implements AbstractTarget {

	/**
	 * 日志
	 */
	static final Logger m_logger = Logger.getLogger(RDBTarget.class.getName());
	
	/**
	 * 
	 * 默认数据库增删改查管理器
	 */
	protected EntityManager entityManager;
	
	/**
	 * @param jdbcUrl        jdbc地址
	 * @param username       用户名
	 * @param password       密码
	 * @param jdbcDriver     jdbc驱动
	 * @param poolSize       jdbc线程池大小
	 */
	protected RDBTarget(String jdbcUrl, String username, String password, String jdbcDriver, String poolSize) {
		try {
			Configuration configuration = new Configuration();
	        // 设置数据库连接信息
	        configuration.setProperty("hibernate.connection.url", jdbcUrl);
	        configuration.setProperty("hibernate.connection.username", username);
	        configuration.setProperty("hibernate.connection.password", password);
	        configuration.setProperty("hibernate.connection.driver_class", jdbcDriver);
	        configuration.setProperty("hibernate.connection.pool_size", poolSize);
	        configuration.setProperty("hibernate.hikari.idleTimeout", "0");
	        configuration.setProperty("hibernate.hikari.connectionTestQuery", "SELECT 1");
	        configuration.setProperty("hibernate.hikari.maxLifetime", "0");
	        
	        // 构建SessionFactory
	        SessionFactory sessionFactory = configuration.buildSessionFactory();
	        // 创建Session对象
	        Session session = sessionFactory.openSession();
	        this.entityManager = session.getEntityManagerFactory().createEntityManager();
		} catch (Exception ex) {
			m_logger.severe("wrong database parameters，or unavailable network." + ex);
			System.exit(1);
		} 
	}
	
	/**
	 * @return   数据库增删改查管理器
	 */
	public EntityManager getEntityManager() {
		return entityManager;
	}
	

	public abstract long count(String sql);
	
	public abstract JsonNode get(String sql);
	
	public abstract JsonNode list(String sql);
	
	public abstract boolean execWithoutResult(String sql);
}
